@extends('admin.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group ">
                @can('member.showings.destroy')
                    <button class="layui-btn layui-btn-sm layui-btn-danger" id="listDelete">删 除</button>
                @endcan
            </div>
            <div class="layui-form" >
                <div class="layui-input-inline">
                    <select name="type" id="f_type">
                        <option value=""></option>
                        <option value="new">新房</option>
                        <option value="hand">二手房</option>
                        <option value="rental">租房</option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="status" id="f_status">
                        <option value=""></option>
                        <option value="F">待处理</option>
                        <option value="D">处理中</option>
                        <option value="T">已完成</option>
                    </select>
                </div>
                <button class="layui-btn layui-btn-normal" id="searchBtn">搜 索</button>
                <button class="layui-btn layui-btn-danger" id="clearBtn">清空筛选</button>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    @can('member.showings.edit')
                        <a class="layui-btn layui-btn-sm" lay-event="edit">查看</a>
                    @endcan
                    @can('member.showings.destroy')
                        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
                    @endcan
                </div>
            </script>
            <script type="text/html" id="showtime">
                @{{ d.show_date }} @{{ d.show_time }}
            </script>
            <script type="text/html" id="type">
                @{{# if(d.type == 'new'){ }}
                    新房
                @{{# }else if(d.type == 'hand'){ }}
                    二手房
                @{{# }else if(d.type == 'rental'){ }}
                    租房
                @{{# }; }}
            </script>
            <script type="text/html" id="status">
                @{{# if(d.status == 'T'){ }}
                    <span class="ok">已完成</span>
                @{{# }else if(d.status == 'D'){ }}
                    <span class="doing">处理中...</span>
                @{{# }else if(d.status == 'F'){ }}
                    <span class="no">待处理</span>
                @{{# }; }}
            </script>
        </div>
    </div>
@endsection

@section('script')
    <style>
        .no{color:#f00;}
        .doing{color:#00a0e9;}
        .ok{color:#ccc;}
    </style>
    @can('member.showings')
        <script>
            layui.use(['layer','table','form'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    ,height: 500
                    ,url: "{{route('admin.member.showings.data')}}" //数据接口
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {checkbox: true,fixed: true}
                        ,{field: 'id', title: 'ID', sort: true,width:80}
                        ,{field: 'title',width:200, title: '楼盘/房源'}
                        ,{field: 'type', title: '类型',toolbar:'#type'}
                        ,{field: 'name', title: '姓名'}
                        ,{field: 'tel', title: '电话'}
                        ,{field: 'showtime', title: '看房时间',toolbar:'#showtime'}
                        ,{field: 'message', title: '其他说明'}
                        ,{field: 'status', title: '状态',toolbar:'#status'}
                        ,{field: 'agent_id', title: '经纪人',toolbar:'#agent'}
                        ,{fixed: 'right', width: 150, align:'center', toolbar: '#options'}
                    ]]
                });

                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'del'){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.member.showings.destroy') }}",{_method:'delete',ids:[data.id]},function (result) {
                                if (result.code==0){
                                    obj.del(); //删除对应行（tr）的DOM结构
                                }
                                layer.close(index);
                                layer.msg(result.msg)
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/member/showings/'+data.id+'/edit';
                    }
                });

                @can('member.showings.edit')
                //监听是否显示
                form.on('switch(isShow)', function(obj){
                    var index = layer.load();
                    var url = $(obj.elem).attr('url')
                    var data = {
                        "is_show" : obj.elem.checked==true?1:0,
                        "_method" : "put"
                    }
                    $.post(url,data,function (res) {
                        layer.close(index)
                        layer.msg(res.msg)
                    },'json');
                });
                @endcan

                //按钮批量删除
                $("#listDelete").click(function () {
                    var ids = []
                    var hasCheck = table.checkStatus('dataTable')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length>0){
                        $.each(hasCheckData,function (index,element) {
                            ids.push(element.id)
                        })
                    }
                    if (ids.length>0){
                        layer.confirm('确认删除吗？', function(index){
                            $.post("{{ route('admin.member.showings.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code==0){
                                    dataTable.reload()
                                }
                                layer.close(index);
                                layer.msg(result.msg)
                            });
                        })
                    }else {
                        layer.msg('请选择删除项')
                    }
                })

                //搜索
                $("#searchBtn").click(function () {
                    var type = $("#f_type").val();
                    var status = $("#f_status").val();
                    dataTable.reload({
                        where:{type:type,status:status},
                        page:{curr:1}
                    })
                })

                $("#clearBtn").click(function () {
                    window.location.reload()
                })
            })
        </script>
    @endcan
@endsection